package com.market.manager.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.market.dao.PurchaseDao;
import com.market.dao.PurchaseDetailDao;
import com.market.dao.SupplierDao;
import com.market.domain.Purchase;
import com.market.domain.Purchase.STATUS;
import com.market.domain.PurchaseDetail;
import com.market.domain.Supplier;
import com.market.manager.PurchaseManager;

@Service("purchaseManager")
public class PurchaseManagerImpl extends
		GenericManagerImpl<PurchaseDao, Purchase, Long> implements
		PurchaseManager {

	@Autowired
	private PurchaseDetailDao purchaseDetailDao;

	@Autowired
	private SupplierDao supplierDao;

	@Autowired
	@Override
	public void setDao(PurchaseDao dao) {
		this.dao = dao;
	}

	public Purchase fetchOne(Purchase bo) {
		Purchase purchase = this.dao.findById(bo.getId());
		purchase.setPurchaseDetails(this.fetchDetails(purchase));
		return purchase;
	}

	public List<PurchaseDetail> fetchDetails(Purchase purchase) {
		return this.dao.findPurchaseDetails(purchase);
	}

	public Purchase confirm(Purchase purchase) {
		purchase.setStatus(STATUS.CONFIRMED);
		purchase.setConfirmedDate(new Date());
		return this.dao.save(purchase);
	}
	
	

	public Purchase unConfirm(Purchase purchase) {
		purchase.setStatus(STATUS.PURCHASED);
		purchase.setConfirmedDate(null);
		return this.dao.save(purchase);
	}

	public Purchase receive(Purchase purchase) {
		purchase.setStatus(STATUS.RECEIVED);
		purchase.setReceivedDate(new Date());
		return this.dao.save(purchase);
	}
	
	

	public Purchase unReceive(Purchase purchase) {
		purchase.setStatus(STATUS.CONFIRMED);
		purchase.setReceivedDate(null);
		return this.dao.save(purchase);
	}

	public Purchase process(Purchase purchase) {
		purchase.setStatus(STATUS.PROCESSED);
		purchase.setProcessedDate(new Date());
		for (PurchaseDetail purchaseDetail : purchase.getPurchaseDetails()) {
			this.purchaseDetailDao.process(purchaseDetail);
		}
		return this.dao.save(purchase);
	}

	
	
	public Purchase unProcess(Purchase purchase) {
		purchase.setStatus(STATUS.RECEIVED);
		purchase.setProcessedDate(null);
		for (PurchaseDetail purchaseDetail : purchase.getPurchaseDetails()) {
			this.purchaseDetailDao.unProcess(purchaseDetail);
		}
		return this.dao.save(purchase);
	}

	public Purchase pay(Purchase purchase) {
		purchase.setStatus(STATUS.PAID);
		purchase.setPaidDate(new Date());
		Double due = purchase.getReceivedTotalAmount()
				- (purchase.getPaidAmount() + purchase.getDiscountAmount());
		Supplier supplier = this.supplierDao.findById(purchase.getSupplier()
				.getId());
		if (due != 0D) {
			supplier.setBalance(supplier.getBalance() + due);
			this.supplierDao.save(supplier);
		}
		purchase.setSupplier(supplier);
		return super.store(purchase);
	}

	public Purchase cancel(Purchase purchase) {
		purchase.setStatus(STATUS.CANCELED);
		purchase.setCanceledDate(new Date());
		return super.store(purchase);
	}

}
